package com.szxy.miaosha.controller;

import com.szxy.miaosha.exception.GlobalException;
import com.szxy.miaosha.service.MiaoShaUserService;
import com.szxy.miaosha.utils.CodeMsg;
import com.szxy.miaosha.utils.Result;
import com.szxy.miaosha.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Author: zwz
 * @Date:2020/9/6 21:23
 * @Description:com.szxy.miaosha.controller
 * @Version:1.0
 **/
@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private MiaoShaUserService miaoShaUserService;

    /**
     * 展示登录页面
     *
     * @return
     */
    @GetMapping("/toPage")
    public String toPage() {
        return "login";
    }

    @PostMapping("/do_login")
    @ResponseBody
    public Result doLogin(@Valid LoginVo loginVo, HttpServletResponse resp) {
        if (loginVo == null) {
            throw new GlobalException(CodeMsg.LOGINVO_NULL_ERROR);
        }
        String token = miaoShaUserService.login(loginVo, resp);
        return Result.success(token);
    }
}
